package com.lsy.weizhi2.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.myutils.ImageAdapterCall;
import com.lsy.weizhi2.myutils.SendPhotoCall;
import com.lsy.weizhi2.picture.PicturePopup;
import com.lsy.weizhi2.utils.MyOkHttpUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * Created by 嗨哟 on 2017/3/18.
 */
public class ImageAdapter2 extends BaseAdapter {
    private Context context;
    private List<?>list;
    private LayoutInflater layoutInflater;
    private ImageAdapterCall imageAdapterCall;
    private List<Integer>list_id;
    private int x;//图片数量
    public ImageAdapter2(Context context, List<?> list) {
        this.context = context;
        this.list = list;
        layoutInflater=LayoutInflater.from(context);
    }

    public void setImageAdapterCall(ImageAdapterCall imageAdapterCall){
        this.imageAdapterCall=imageAdapterCall;
    }

    public void UpData(List<Integer>list_id){
        this.list_id=list_id;
        this.notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ImageHolder holder;
        if(view==null){
            view=layoutInflater.inflate(R.layout.imageitem2,viewGroup,false);
            holder=new ImageHolder();
            holder.imageitem2= (ImageView) view.findViewById(R.id.imageitem2);
            holder.delete= (ImageView) view.findViewById(R.id.delete);
            AutoUtils.autoSize(view);
            view.setTag(holder);
        }else{
            holder= (ImageHolder) view.getTag();
        }
        if(list_id!=null){
            x=list_id.size();
        }
        if(i>=x){
            holder.imageitem2.setImageResource(R.mipmap.image_add);
            holder.delete.setVisibility(View.GONE);
            holder.imageitem2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    imageAdapterCall.onClick(view,x);
                }
            });
        }else{
            holder.delete.setVisibility(View.VISIBLE);
            Glide.with(context)
                    .load(new GlideUrl(Constants.DOWN_USER_PICTURE+list_id.get(i),new LazyHeaders.Builder().addHeader("WZ-AUTH-TOKEN", MyInfo.Token).addHeader("WZ-AUTH-ID",MyInfo.User_Id+"").build()))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(holder.imageitem2);
        }
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imageAdapterCall.onDetele(view,i);
            }
        });
        return view;
    }

    class ImageHolder{
        ImageView imageitem2,delete;
    }
}
